# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>
# Contributor: Simon Sobisch <simonsobisch@gnu.org>

_realname=gnucobol
_realver=3.2-rc1
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=${_realver//-/_}   # not necessarily matches as the rule for the version identifier differ
pkgrel=1
pkgdesc="GnuCOBOL, a free and modern COBOL compiler (mingw-w64)"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn" "${MINGW_PACKAGE_PREFIX}-gnu-cobol-svn")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn" "${MINGW_PACKAGE_PREFIX}-gnu-cobol-svn")
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.gnu.org/software/gnucobol/"
options=('strip')
license=('GPL3' 'LGPL3')
# note: the final binaries will call GCC, therefore it is a dependency
depends=("${MINGW_PACKAGE_PREFIX}-cc"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-cjson"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-db")

# the optional depencies cannot be used for creating the binary packages, moved to depends
#optdepends=("${MINGW_PACKAGE_PREFIX}-gettext: support for internationalization"
#            "${MINGW_PACKAGE_PREFIX}-ncurses: support for extended screenio"
#            "${MINGW_PACKAGE_PREFIX}-pdcurses: support for screenio"
#            "${MINGW_PACKAGE_PREFIX}-cjson: support for JSON GENERATE"
#            "${MINGW_PACKAGE_PREFIX}-json-c: support for JSON GENERATE"
#            "${MINGW_PACKAGE_PREFIX}-libxml2: support for XML GENERATE")
# Note: any autotools would work, but for multiple packages the common one would be best
makedepends=(autotools "${MINGW_PACKAGE_PREFIX}-cc")
# Note: it actually needs the one from the build environment (MSYS)
checkdepends=(perl) 

# local definitions
source=("https://alpha.gnu.org/gnu/${_realname}/${_realname}-${_realver}.tar.xz"{,.sig}
#source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${_realver}.tar.xz"{,.sig}
        "cobenv.sh" "cobenv.cmd"
        "https://www.itl.nist.gov/div897/ctg/suites/newcob.val.Z"
        )
sha256sums=('026E01480FA91FAE2B53C20BD118C133234FE1B07409E144FDBE5E17E5A8E6E7'
            SKIP
            'EEDFC170CFD6606527DD701C3CF6BBA2029C33CE694F697F8B7EE527B4ED7F1C'
            '244AFBD011B0C0141753C7259173D9F49F161A97C7252B52369E0CEC50147308'
            '1E9A92DDBD5D730CBEB764281F7810C22B18E0163985B09675393AB22BBD61F9'
            )
validpgpkeys=('B9459D0CA8A740B323235CDF13E96B53C005604E')

prepare() {
  cd "${_realname}-${_realver}"
  cp ${srcdir}/newcob.val.Z tests/cobol85/
}

build() {
  export lt_cv_deplibs_check_method='pass_all'   # libncurses with unconforming name
  cd ${srcdir}/${_realname}-${_realver}
  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir build-${CARCH}
  cd build-${CARCH}
  ../configure \
    CPPLAGS="-D__USE_MINGW_ANSI_STDIO=1" \
    PERL="/usr/bin/perl" \
    --with-db --without-vbisam --without-disam --without-cisam \
    --with-xml2 --with-json=cjson --with-curses=ncursesw \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --infodir=${MINGW_PREFIX}/share/info \
    --mandir=${MINGW_PREFIX}/share/man \
    --disable-rpath \
    --enable-shared \
    --disable-static
    #  --> as using the static libcob would be the builtin default,
    #      all generated COBOL modules would be GPLed (and big)...
    #      ... and only work when all linked together

  make
}

check() {
  cd ${srcdir}/${_realname}-${_realver}/build-${CARCH}
  # note: strangely multiple tests seem to halt the complete package creation
  #       in any case the parameter should be resolved from the common makevars
  #       of the system (check for --jobs=N / -jN and take N over here)
  #make check TESTSUITEFLAGS="--jobs=$(($(nproc)+1))"
  make check || echo "warning, not all internal tests passed"
  PATH=/usr/bin:$PATH make test  || echo "warning, not all NIST tests passed"
}

package() {
  # install package previously build
  cd ${srcdir}/${_realname}-${_realver}/build-${CARCH}
  make DESTDIR="${pkgdir}" install

  # copy license files and docs from source to expected places
  cd ..
  install -d "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -p -m644 COPYING* "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  install -p -m644 README NEWS "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cd doc
  install -p -m644 gnucobol*.pdf "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"

  # copy extra files
  install -p -m777 ${srcdir}/cobenv.* "${pkgdir}${MINGW_PREFIX}/bin"
}
